<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>检查变量是否为空</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.doubleval.html">« doubleval</a></li>
      <li style="float: right;"><a href="function.floatval.html">floatval »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.var.html">Variable handling 函数</a></li>
    <li>检查变量是否为空</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.empty" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">empty</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">empty</span> &mdash; <span class="dc-title">检查变量是否为空</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.empty-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>empty</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span> <code class="parameter">$var</code></span>): <span class="type">bool</span></div>

  <p class="para rdfs-comment">
   判断变量是否为是空。当变量不存在或者它的值等于
   <strong><code>false</code></strong>，那么视为空。如果变量不存在，<span class="function"><strong>empty()</strong></span> 不会产生警告。
  </p>
 </div>

 <div class="refsect1 parameters" id="refsect1-function.empty-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">var</code></dt>

     <dd>

      <p class="para">
       待检查的变量
      </p>
      <p class="para">
       没有警告会产生，哪怕变量并不存在。这意味着 <span class="function"><strong>empty()</strong></span> 本质上与
       <strong class="command">!isset($var) || $var == false</strong> 等价。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>

 <div class="refsect1 returnvalues" id="refsect1-function.empty-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   当 <code class="parameter">var</code> 不存在、值为空、等于 0、为 false 时，返回 <strong><code>true</code></strong>。参阅 <a href="language.types.boolean.html#language.types.boolean.casting" class="link">转换为 boolean</a>。否则返回 <strong><code>false</code></strong>。
   否则返回 <strong><code>true</code></strong>。
  </p>
 </div>

 <div class="refsect1 examples" id="refsect1-function.empty-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-5271">
    <p><strong>示例 #1 
     一个简单的 <span class="function"><strong>empty()</strong></span> 与 <span class="function"><a href="function.isset.html" class="function">isset()</a></span> 的比较。
    </strong></p>  
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$var&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;因为&nbsp;$var&nbsp;为空，所以计算结果为&nbsp;true<br /></span><span style="color: #007700">if&nbsp;(empty(</span><span style="color: #0000BB">$var</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'$var&nbsp;is&nbsp;either&nbsp;0,&nbsp;empty,&nbsp;or&nbsp;not&nbsp;set&nbsp;at&nbsp;all'</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #FF8000">//&nbsp;因为&nbsp;$var&nbsp;已赋值，所以计算结果为&nbsp;true<br /></span><span style="color: #007700">if&nbsp;(isset(</span><span style="color: #0000BB">$var</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'$var&nbsp;is&nbsp;set&nbsp;even&nbsp;though&nbsp;it&nbsp;is&nbsp;empty'</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
  <div class="example" id="example-5272">
   <p><strong>示例 #2 在字符串偏移量上使用 <span class="function"><strong>empty()</strong></span></strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$expected_array_got_string&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'somestring'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(empty(</span><span style="color: #0000BB">$expected_array_got_string</span><span style="color: #007700">[</span><span style="color: #DD0000">'some_key'</span><span style="color: #007700">]));<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(empty(</span><span style="color: #0000BB">$expected_array_got_string</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]));<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(empty(</span><span style="color: #0000BB">$expected_array_got_string</span><span style="color: #007700">[</span><span style="color: #DD0000">'0'</span><span style="color: #007700">]));<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(empty(</span><span style="color: #0000BB">$expected_array_got_string</span><span style="color: #007700">[</span><span style="color: #0000BB">0.5</span><span style="color: #007700">]));<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(empty(</span><span style="color: #0000BB">$expected_array_got_string</span><span style="color: #007700">[</span><span style="color: #DD0000">'0.5'</span><span style="color: #007700">]));<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(empty(</span><span style="color: #0000BB">$expected_array_got_string</span><span style="color: #007700">[</span><span style="color: #DD0000">'0&nbsp;Mostel'</span><span style="color: #007700">]));<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>以上例程会输出：</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
bool(true)
bool(false)
bool(false)
bool(false)
bool(true)
bool(true)
</pre></div>
   </div>
  </div>
 </div>

 <div class="refsect1 notes" id="refsect1-function.empty-notes">
  <h3 class="title">注释</h3>
  <blockquote class="note"><p><strong class="note">注意</strong>: <span class="simpara">因为是语言构造器而不是函数，不能被
<a href="functions.variable-functions.html" class="link">可变函数</a> 或者
<a href="functions.arguments.html#functions.named-arguments" class="link">命名参数</a> 调用。
</span>
</p></blockquote>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
    当对一个不可见的对象属性使用 <span class="function"><strong>empty()</strong></span> 时，如果存在
    <a href="language.oop5.overloading.html#object.isset" class="link">__isset()</a> 方法，它将会被调用。
   </p>
  </p></blockquote>
 </div>

 <div class="refsect1 seealso" id="refsect1-function.empty-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.isset.html" class="function" rel="rdfs-seeAlso">isset()</a> - 检测变量是否已声明并且其值不为 null</span></li>
    <li class="member"><a href="language.oop5.overloading.html#object.isset" class="link">__isset()</a></li>
    <li class="member"><span class="function"><a href="function.unset.html" class="function" rel="rdfs-seeAlso">unset()</a> - 清除指定变量</span></li>
    <li class="member"><span class="function"><a href="function.array-key-exists.html" class="function" rel="rdfs-seeAlso">array_key_exists()</a> - 检查数组里是否有指定的键名或索引</span></li>
    <li class="member"><span class="function"><a href="function.count.html" class="function" rel="rdfs-seeAlso">count()</a> - 统计数组、Countable 对象中所有元素的数量</span></li> 
    <li class="member"><span class="function"><a href="function.strlen.html" class="function" rel="rdfs-seeAlso">strlen()</a> - 获取字符串长度</span></li>
    <li class="member"><a href="types.comparisons.html" class="link">类型比较表</a></li>
   </ul>
  </p>
 </div>

</div></div></div></body></html>